using System.Collections.Generic;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;

namespace ShapeEditor
{
    public partial class ClippedDrawableControl : UserControl
    {
        private ClippedDrawableProxy clippedDrawableProxy;
        private AssetManager assetMan;

        private delegate void ClippedDrawableHandler(ClippedDrawableProxy cdp);
        public ClippedDrawableControl()
        {
            InitializeComponent();
            assetMan = AssetManager.Instance;
            assetManagerDropDownControl1.Init(typeof(IDrawable));
            assetManagerDropDownControl2.Init(typeof(ClipPlaneProxy));
            assetManagerDropDownControl3.Init(typeof(ClipPlaneProxy));
            assetManagerDropDownControl4.Init(typeof(ClipPlaneProxy));

            assetManagerDropDownControl1.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl1_SelectedEvent);
            assetManagerDropDownControl2.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl2_SelectedEvent);
            assetManagerDropDownControl3.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl3_SelectedEvent);
            assetManagerDropDownControl4.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl4_SelectedEvent);
            
        }

        public ClippedDrawableProxy ClippedDrawProxy
        {
            get { return clippedDrawableProxy; }
        }

        public void SetClippedDrawProxy_thisThread(ClippedDrawableProxy ClippedDrawProxy)
        {
            clippedDrawableProxy = null;

            if (ClippedDrawProxy == null)
            {
                this.Enabled = false;
            }
            else
            {
                this.Enabled = true;
                IDrawable idraw = ClippedDrawProxy.Drawable;
                assetManagerDropDownControl1.SelectItem((IAssetManaged)idraw);

                ClipPlaneProxy cpp1 = ClippedDrawProxy.ClipPlanes[0];
                assetManagerDropDownControl2.SelectItem(cpp1);

                ClipPlaneProxy cpp2 = ClippedDrawProxy.ClipPlanes[1];
                assetManagerDropDownControl3.SelectItem(cpp2);

                ClipPlaneProxy cpp3 = ClippedDrawProxy.ClipPlanes[2];
                assetManagerDropDownControl4.SelectItem(cpp3);
            }
            clippedDrawableProxy = ClippedDrawProxy;
        }
        public void SetClippedDrawProxy(ClippedDrawableProxy ClippedDrawProxy)
        {
            ClippedDrawableHandler newEvent = new ClippedDrawableHandler(SetClippedDrawProxy_thisThread);
            object[] args = new object[1];
            args[0] = ClippedDrawProxy;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(newEvent, args);
                else
                    newEvent(ClippedDrawProxy);
            }
            catch { }
        }

        private void assetManagerDropDownControl1_SelectedEvent(IAssetManaged obj)
        {
            if (clippedDrawableProxy != null /*&& !((obj is ClippedDrawableProxy) || (obj is LayerProxy))*/)
            {
                clippedDrawableProxy.Drawable = (IDrawable)obj;
            }
        }

        private void assetManagerDropDownControl2_SelectedEvent(IAssetManaged obj)
        {
            if (clippedDrawableProxy != null)
            {
                clippedDrawableProxy.ClipPlanes[0] = (ClipPlaneProxy)obj;
            }
        }

        private void assetManagerDropDownControl3_SelectedEvent(IAssetManaged obj)
        {
            if (clippedDrawableProxy != null)
            {
                clippedDrawableProxy.ClipPlanes[1] = (ClipPlaneProxy)obj;
            }
        }
        private void assetManagerDropDownControl4_SelectedEvent(IAssetManaged obj)
        {
            if (clippedDrawableProxy != null)
            {
                clippedDrawableProxy.ClipPlanes[2] = (ClipPlaneProxy)obj;
            }
        }

        public IDrawable SelectDrawableDefault()
        {
            List<IDrawable> iDrawables = new List<IDrawable>();
            // Hack for now only SharedBox
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(IDrawable));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is IDrawable && !(obj is LayerProxy))
                {
                    iDrawables.Add((IDrawable)obj);
                }
            }
            if (iDrawables.Count > 0 && iDrawables[0] != null)
            {
                assetManagerDropDownControl1.SelectItem((IAssetManaged)iDrawables[0]);
                return iDrawables[0];
            }

            return null;
        }

        public ClipPlaneProxy SelectClipPlane1Default()
        {
            ClipPlaneProxy clipPlane = null; ;
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(ClipPlaneProxy));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is ClipPlaneProxy)
                {
                    clipPlane = (ClipPlaneProxy)obj;
                    break;
                }
            }
            if (clipPlane != null)
            {
                assetManagerDropDownControl2.SelectItem((IAssetManaged)clipPlane);
                return clipPlane;
            }

            return null;
        }
        public ClipPlaneProxy SelectClipPlane2Default()
        {
            ClipPlaneProxy clipPlane = null; ;
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(ClipPlaneProxy));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is ClipPlaneProxy)
                {
                    clipPlane = (ClipPlaneProxy)obj;
                    break;
                }
            }
            if (clipPlane != null)
            {
                assetManagerDropDownControl3.SelectItem((IAssetManaged)clipPlane);
                return clipPlane;
            }

            return null;
        }
        public ClipPlaneProxy SelectClipPlane3Default()
        {
            ClipPlaneProxy clipPlane = null; ;
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(ClipPlaneProxy));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is ClipPlaneProxy)
                {
                    clipPlane = (ClipPlaneProxy)obj;
                    break;
                }
            }
            if (clipPlane != null)
            {
                assetManagerDropDownControl4.SelectItem((IAssetManaged)clipPlane);
                return clipPlane;
            }

            return null;
        }
      

        public delegate void ClippedDrawableVisibility(bool isVisible);

        public void Visibility(bool isVisible)
        {
            ClippedDrawableVisibility clippedDrawableVisibility = new ClippedDrawableVisibility(IsVisible_ThisThread);

            object[] args = new object[1];
            args[0] = isVisible;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(clippedDrawableVisibility, args);
                else
                    clippedDrawableVisibility(isVisible);
            }
            catch { }

        }

        public void IsVisible_ThisThread(bool isVisible)
        {
            this.Visible = isVisible;
        }
    }
}
